package qqclient.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author 王艺锡
 * @version 1.0
 * 该类用于编写聊天相关的方法
 */
public class MessageClientService {

    public void PrivateChat(String senderId,String receiverId,String content){
       //获取当前时间
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(date);

        //设置信息
        Message message = new Message();
        message.setSender(senderId);
        message.setReceiver(receiverId);
        message.setContent(content);
        message.setSendTime(format);
        message.setMesType(MessageType.MESSAGE_COMM_MES);

        //获取发送者的socket
        Socket socket =
                ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(message.getSendTime());
        System.out.println("你对" + receiverId + "说:" + content);

    }

    public void massChat(String senderId,String content){
       //获取当前时间
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(date);

        //设置消息详细信息
        Message message = new Message();
        message.setSendTime(format);
        message.setSender(senderId);
        message.setContent(content);
        message.setMesType(MessageType.MESSAGE_MASS_MES);

        //获取发送者的socket
        Socket socket =
                ManageClientConnectServerThread
                        .getClientConnectServerThread(senderId)
                        .getSocket();

        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(message.getSendTime());
        System.out.println("你对" + "大家说:" + content);

    }

}
